//
// Created by dcang on 2021/7/9.
//
#include <jni.h>
#include <stddef.h>
#include "AndroidLog.h"
#include "YcJavaCall.h"
#include "YcFFmpeg.h"


_JavaVM *javaVM = NULL;
YcJavaCall *ycJavaCall = NULL;
YcFFmpeg *ycFFmpeg = NULL;

extern "C"
JNIEXPORT void JNICALL
Java_com_ycplayerlib_ycplayer_YCPlayer_ycPrepared(JNIEnv *env, jobject instance, jstring url_, jboolean isOnlyMusic) {
//    const char *url1 = env->GetStringUTFChars(url_,0);
//    const char* input_cstr = (*env)->GetStringUTFChars(env,url_,NULL);
    const char* url = (*env).GetStringUTFChars(url_,NULL);
    if(ycJavaCall == NULL){
        ycJavaCall = new YcJavaCall(javaVM,env,&instance);
    }

    if(ycFFmpeg == NULL){
        ycFFmpeg = new YcFFmpeg(ycJavaCall,url,isOnlyMusic);
        ycJavaCall->onLoad(YC_THREAD_MAIN,true);
        ycFFmpeg->preparedFFmpeg();
    }

}

extern "C"
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
{
    jint result = -1;
    javaVM =  vm;
    JNIEnv* env;

    if(vm->GetEnv((void **)&env,JNI_VERSION_1_4) != JNI_OK){
        if(LOG_SHOW)
        {
            LOGE("GetEnv failed!");
        }
        return result;
    }
    return JNI_VERSION_1_4;
}

extern "C"
JNIEXPORT void JNICALL
Java_com_ycplayerlib_ycplayer_YCPlayer_ycStart(JNIEnv *env, jobject instance){
    if(ycFFmpeg != NULL){
        ycFFmpeg->start();
    }
}

extern "C"
JNIEXPORT void JNICALL
Java_com_ycplayerlib_ycplayer_YCPlayer_ycStop(JNIEnv *env, jobject instance, jboolean exit){/*bool exit*/


    if(ycFFmpeg != NULL){
        ycFFmpeg->exitByUser = true;
        ycFFmpeg->release();
        delete(ycFFmpeg);
        ycFFmpeg = NULL;

        if(ycJavaCall != NULL){
            ycJavaCall->release();
            ycJavaCall = NULL;
        }

        if(!exit){
            jclass jlz = env->GetObjectClass(instance);
            jmethodID jmid_stop = env->GetMethodID(jlz,"onStopComplete", "()V");
            env->CallVoidMethod(instance,jmid_stop);
        }

    }

}

extern "C"
JNIEXPORT void JNICALL
Java_com_ycplayerlib_ycplayer_YCPlayer_ycPause(JNIEnv *env, jobject instance){
    if(ycFFmpeg != NULL){
        ycFFmpeg->pause();
    }
}


extern "C"
JNIEXPORT void JNICALL
Java_com_ycplayerlib_ycplayer_YCPlayer_ycResume(JNIEnv *env, jobject instance) {

    if(ycFFmpeg != NULL){
        ycFFmpeg->resume();
    }
}

extern "C"
JNIEXPORT void JNICALL
Java_com_ycplayerlib_ycplayer_YCPlayer_ycSeek(JNIEnv *env, jobject instance, jint secds) {

    if(ycFFmpeg != NULL){
        ycFFmpeg->seek(secds);
    }

}


extern "C"
JNIEXPORT jint JNICALL
Java_com_ycplayerlib_ycplayer_YCPlayer_ycGetDuration(JNIEnv *env, jobject instance) {

    if(ycFFmpeg != NULL){
        return ycFFmpeg->getDuration();
    }
    return 0;
}


extern "C"
JNIEXPORT jint JNICALL
Java_com_ycplayerlib_ycplayer_YCPlayer_ycGetAudioChannels(JNIEnv *env, jobject instance) {
    if(ycFFmpeg != NULL){
        return ycFFmpeg->getAudioChannels();
    }
    return 0;
}


extern "C"
JNIEXPORT jint JNICALL
Java_com_ycplayerlib_ycplayer_YCPlayer_ycGetVideoWidth(JNIEnv *env, jobject instance) {

    if(ycFFmpeg != NULL){
        return ycFFmpeg->getVideoWidth();
    }
    return 0;
}


extern "C"
JNIEXPORT jint JNICALL
Java_com_ycplayerlib_ycplayer_YCPlayer_ycGetVideoHeidht(JNIEnv *env, jobject instance) {


    if(ycFFmpeg != NULL){
        return ycFFmpeg->getVideoHeight();
    }
    return 0;

}

extern "C"
JNIEXPORT void JNICALL
Java_com_ycplayerlib_ycplayer_YCPlayer_ycSetAudioChannels(JNIEnv *env, jobject instance, jint index) {


    if(ycFFmpeg != NULL){
        return ycFFmpeg->setAudioChannel(index);
    }
}